import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import Item from '../components/items/Item.vue'
import AddItem from '../components/items/addItem.vue'
import ItemCat from '../components/items/ItemCat.vue'
import ItemCatParam from '../components/items/ItemCatParam.vue'
import Rights from '../components/rights/Rights.vue'
import Role from '../components/rights/Role.vue'
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/home', component: Home, redirect: '/user',
    children: [
      {path: '/user', component: User},
      {path: '/item', component: Item},
      {path: '/itemCat', component: ItemCat},
      {path: '/itemCatParam', component: ItemCatParam},
      {path: '/item/addItem', component: AddItem},
      {path: '/rights', component: Rights},
      {path: '/role', component: Role},
      {path: '/report', component: Report},
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router

router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next()
  //如果用户访问的网址不是/login获取token
  let token = window.sessionStorage.getItem("token")
  if (token) return next()
  //如果程序执行至这——行说明token没有数据.跳转到登味页面
  next('/login')
})
